/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Linq;
using System.Diagnostics;
using Xceed.Silverlight.Data.Stats;
using System.Collections.Generic;

namespace Xceed.Silverlight.Data
{
  internal class DataPathFactory : DataItemFactory<DataPath>
  {

    public DataPathFactory(GroupHelper groupHelper)
    {
      if( groupHelper == null )
        throw new ArgumentNullException( "groupHelper" );

      m_groupHelper = groupHelper;
    }
    public int PathDepth
    {
      get
      {
        return m_groupHelper.GroupHandler.GroupDescriptions.Count + 1;
      }
    }

    protected internal override StatResults CreateDummyStatResults( StatParameters parameters )
    {
      List<StatResult> dummyResults = new List<StatResult>();

      foreach( StatFunction func in parameters.StatFunctions )
      {
        dummyResults.Add( new StatResult( null, true ) );
      }

      return new StatResults( dummyResults );
    }

    protected internal override DataPath CreateInstance(DataPath templateItem)
    {
      DataItem[] items = new DataItem[ this.PathDepth ];
      bool isUnCommonDescendant = false;
      GroupDataItem group = null;

      for( int i = 0; i < items.Length - 1; i++ )
      {
        group = null;

        if( templateItem != null && i < templateItem.Depth && !isUnCommonDescendant )
        {
          if( templateItem[ i ].IsLoaded )
          {
            DataPath currentTemplatePath = ( i == templateItem.Depth - 1 ) ? templateItem : templateItem.CreateAncestorPath( i + 1 );
            isUnCommonDescendant = true;
            if( m_groupHelper.IsGroupPath( currentTemplatePath ) )
            {
              if( !m_groupHelper.GroupHandler.IsCollapsed( currentTemplatePath ) )
              {
                isUnCommonDescendant = false;
                System.Diagnostics.Debug.Assert( templateItem[ i ] is GroupDataItem );
                group = new GroupDataItem( templateItem[ i ].Data );
              }
            }
          }
          else
          {
            System.Diagnostics.Debug.Assert( templateItem[ i ] is GroupDataItem, "No other type of data is expected. May change with master detail or group headers" );
            group = ( GroupDataItem )templateItem[ i ];
          }
        }

        if( group == null )
        {
          group = new GroupDataItem( null, false );
        }
        items[ i ] = group;
      }

      items[ items.Length - 1 ] = new DataItem();

      return new DataPath( items, true );
    }

    protected internal override bool IsDummy( DataPath item )
    {
      return ( item != null ) && !item.LastChild.IsLoaded;
    }

    protected internal override bool HasParent( DataPath source )
    {
      return ( source.Depth > 1 );
    }

    protected internal override object GetParentValue( DataPath sourceItem, int depth, ref bool leafReached )
    {
      if( depth >= sourceItem.Depth )
        throw new ArgumentOutOfRangeException( "depth", "depth must be less than the depth of the specified DataPath." );

      if( depth == sourceItem.Depth - 1 )
        leafReached = true;

      return sourceItem.CreateAncestorPath( depth );
    }

    protected internal override object GetParentValue( DataPath item )
    {
      return item.ParentPath;
    }

    protected internal override bool IsPartiallyInitialized( DataPath item )
    {
      for( int i = 0; i < item.Depth; i++ )
      {
        if( item[ i ].IsLoaded )
          return true;
      }

      return false;
    }

    protected internal override bool IsCompletelyInitialized( DataPath item )
    {
      for( int i = 0; i < item.Depth; i++ )
      {
        if( !item[ i ].IsLoaded )
          return false;
      }

      return true;
    }

    protected internal override bool IsParentEqualTo( DataPath item, object parentValue )
    {
      return object.Equals( item.ParentPath, parentValue );
    }

    private GroupHelper m_groupHelper;
  }
}
